package com.kay.springboot_es_api.controller;

import com.kay.springboot_es_api.domain.Basic;
import com.kay.springboot_es_api.domain.Filters;
import com.kay.springboot_es_api.utils.FilterUtil;
import com.sun.org.apache.bcel.internal.generic.ReturnInstruction;
import org.frameworkset.elasticsearch.ElasticSearchHelper;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.frameworkset.elasticsearch.entity.ESDatas;
import org.frameworkset.elasticsearch.entity.MapRestResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.sound.midi.Soundbank;
import java.util.HashMap;
import java.util.Map;

@RestController
public class HelloWorldController {
    @GetMapping("/helloworld")
    public String HelloWorld(){

        return "Hello World".toString();
    }

//    @PostMapping("/user")
//    public Object passUser(@RequestBody User user)
//    {
//        return user;
//    }

    @PostMapping("/filter")
    public ESDatas<Basic> passFilter(@RequestBody Filters filter){

        ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/demo.xml");
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("age",filter.getAge());
        params.put("sex",filter.getSex());
        ESDatas<Basic> testdsl = clientUtil.searchList("client_info/basic/_search", "searchClientInfo", params, Basic.class);
        System.out.println(testdsl.getDatas().toString());
        return testdsl;
    }

    @PostMapping("/testfilter")
    public ESDatas<Basic> testFilter(@RequestBody Filters filters){

        ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/demo.xml");
        Map<String,Object> params = FilterUtil.FilterParam(filters);
        ESDatas<Basic> testdsl = clientUtil.searchList("client_info/basic/_search", "searchClientInfo", params, Basic.class);
        System.out.println("basic total : " + testdsl.getTotalSize());
        return testdsl;
//        return filters;
    }
}
